home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / include / Eet.h < prev    next >
Encoding:
C/C++ Source or Header  |  2006-01-09  |  38.1 KB  |  884 lines

  1. #ifndef _EET_H
  2. #define _EET_H
  3.  
  4. #include <stdlib.h>
  5.  
  6. #ifdef EAPI
  7. #undef EAPI
  8. #endif
  9. #ifdef WIN32
  10. # ifdef BUILDING_DLL
  11. #  define EAPI __declspec(dllexport)
  12. # else
  13. #  define EAPI __declspec(dllimport)
  14. # endif
  15. #else
  16. # ifdef __GNUC__
  17. #  if __GNUC__ >= 4
  18. #   define EAPI __attribute__ ((visibility("default")))
  19. #  else
  20. #   define EAPI
  21. #  endif
  22. # else
  23. #  define EAPI
  24. # endif
  25. #endif
  26.  
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30.  
  31. /**
  32.  * @file Eet.h
  33.  * @brief The file that provides the eet functions.
  34.  *
  35.  * This header provides the Eet management functions.
  36.  *
  37.  */
  38.  
  39. /***************************************************************************/
  40.  
  41. #define EET_T_UNKNOW     0 /**< Unknown data encding type */
  42. #define EET_T_CHAR       1 /**< Data type: char */
  43. #define EET_T_SHORT      2 /**< Data type: short */
  44. #define EET_T_INT        3 /**< Data type: int */
  45. #define EET_T_LONG_LONG  4 /**< Data type: long long */
  46. #define EET_T_FLOAT      5 /**< Data type: float */
  47. #define EET_T_DOUBLE     6 /**< Data type: double */
  48. #define EET_T_UCHAR      7 /**< Data type: unsigned char */
  49. #define EET_T_USHORT     8 /**< Data type: unsigned short */
  50. #define EET_T_UINT       9 /**< Data type: unsigned int */
  51. #define EET_T_ULONG_LONG 10 /**< Data type: unsigned long long */
  52. #define EET_T_STRING     11 /**< Data type: char * */
  53. #define EET_T_LAST       12 /**< Last data type */
  54.  
  55. #define EET_G_UNKNOWN    100 /**< Unknown group data encoding type */
  56. #define EET_G_ARRAY      101 /**< Fixed size array group type */
  57. #define EET_G_VAR_ARRAY  102 /**< Variable size array group type */
  58. #define EET_G_LIST       103 /**< Linked list group type */
  59. #define EET_G_HASH       104 /**< Hash table group type */
  60. #define EET_G_LAST       105 /**< Last group type */
  61.  
  62. /***************************************************************************/
  63.  
  64.    enum _Eet_File_Mode
  65.      {
  66.     EET_FILE_MODE_INVALID = -1,
  67.     EET_FILE_MODE_READ,
  68.     EET_FILE_MODE_WRITE,
  69.     EET_FILE_MODE_READ_WRITE
  70.      };
  71.  
  72.    typedef enum _Eet_File_Mode               Eet_File_Mode;
  73.  
  74.    typedef struct _Eet_File                  Eet_File;
  75.    typedef struct _Eet_Data_Descriptor       Eet_Data_Descriptor;
  76.  
  77.    typedef struct _Eet_Data_Descriptor_Class Eet_Data_Descriptor_Class;
  78.    
  79. #define EET_DATA_DESCRIPTOR_CLASS_VERSION 1
  80.    struct _Eet_Data_Descriptor_Class
  81.      {
  82.     int         version;
  83.     const char *name;
  84.     int         size;
  85.     struct {
  86.        void   *(*mem_alloc) (size_t size);
  87.        void    (*mem_free) (void *mem);
  88.        char   *(*str_alloc) (const char *str);
  89.        void    (*str_free) (const char *str);
  90.        void   *(*list_next) (void *l);
  91.        void   *(*list_append) (void *l, void *d);
  92.        void   *(*list_data) (void *l);
  93.        void   *(*list_free) (void *l);
  94.        void    (*hash_foreach) (void *h, int (*func) (void *h, const char *k, void *dt, void *fdt), void *fdt);
  95.        void   *(*hash_add) (void *h, const char *k, void *d);
  96.        void    (*hash_free) (void *h);
  97.     } func;
  98.      };
  99.    
  100. /***************************************************************************/
  101.  
  102.    /**
  103.     * Initialize the EET library.
  104.     *
  105.     * @return The new init count.
  106.     */
  107.    EAPI int eet_init(void);
  108.  
  109.    /**
  110.     * Shut down the EET library.
  111.     *
  112.     * @return The new init count.
  113.     */
  114.    EAPI int eet_shutdown(void);
  115.  
  116.    /**
  117.     * Turn cacheburst on/off
  118.     *
  119.     * @param on Set this to 1 to turn on, 0 to turn off.
  120.     * 
  121.     * This enables cacheburst mode. This is where eet will not free items from
  122.     * its internal share cache even when their references hit 0. This is
  123.     * intended to be enabled during bursts where eet may open several eet
  124.     * files over and over and over again (eg in initialization of an app) and
  125.     * thius this will avoid repeated openings. It will NOT respect changes
  126.     * on disk and if you open a LOT of files may use a lot of memory.
  127.     */
  128.    EAPI void eet_cacheburst(int on);
  129.  
  130.    /**
  131.     * Open an eet file on disk, and returns a handle to it.
  132.     * @param file The file path to the eet file. eg: "/tmp/file.eet".
  133.     * @param mode The mode for opening. Either EET_FILE_MODE_READ or EET_FILE_MODE_WRITE, but not both.
  134.     * @return An opened eet file handle.
  135.     *
  136.     * This function will open an exiting eet file for reading, and build
  137.     * the directory table in memory and return a handle to the file, if it
  138.     * exists and can be read, and no memory errors occur on the way, otherwise
  139.     * NULL will be returned.
  140.     *
  141.     * It will also open an eet file for writing. This will, if successful,
  142.     * delete the original file and replace it with a new empty file, till
  143.     * the eet file handle is closed or flushed. If it cannot be opened for
  144.     * writing or a memory error occurs, NULL is returned.
  145.     *
  146.     * Example:
  147.     * @code
  148.     * #include <Eet.h>
  149.     *
  150.     * int
  151.     * main(int argc, char **argv)
  152.     * {
  153.     *   Eet_File *ef;
  154.     *   char buf[1024], *ret, **list;
  155.     *   int size, num, i;
  156.     *
  157.     *   strcpy(buf, "Here is a string of data to save!");
  158.     *
  159.     *   ef = eet_open("/tmp/my_file.eet, EET_FILE_MODE_WRITE);
  160.     *   if (!ef) return -1;
  161.     *   if (!eet_write(ef, "/key/to_store/at", buf, 1024, 1))
  162.     *     fprintf("Error writing data!\n");
  163.     *   eet_close(ef);
  164.     *
  165.     *   ef = eet_open("/tmp/my_file.eet, EET_FILE_MODE_READ);
  166.     *   if (!ef) return -1;
  167.     *   list = eet_list(ef, "*", &num);
  168.     *   if (list)
  169.     *     {
  170.     *       for (i = 0; i < num; i++)
  171.     *         printf("Key stored: %s\n", list[i]);
  172.     *       free(list);
  173.     *     }
  174.     *   ret = eet_read(ef, "/key/to_store/at", &size);
  175.     *   if (ret)
  176.     *     {
  177.     *       printf("Data read (%i bytes):\n%s\n", size, ret);
  178.     *       free(ret);
  179.     *     }
  180.     *   eet_close(ef);
  181.     *
  182.     *   return 0;
  183.     * }
  184.     * @endcode
  185.     */
  186.    EAPI Eet_File *eet_open  (const char *file, Eet_File_Mode mode);
  187.  
  188.    /**
  189.     * Get the mode an Eet_File was opened with.
  190.     * @param ef A valid eet file handle.
  191.     * @return The mode ef was opened with.
  192.     */
  193.    EAPI Eet_File_Mode eet_mode_get (Eet_File *ef);
  194.  
  195.    /**
  196.     * Close an eet file handle and flush and writes pending.
  197.     * @param ef A valid eet file handle.
  198.     *
  199.     * This function will flush any pending writes to disk if the eet file
  200.     * was opened for write, and free all data associated with the file handle
  201.     * and file, and close the file.
  202.     *
  203.     * If the eet file handle is not valid nothing will be done.
  204.     */
  205.    EAPI void      eet_close (Eet_File *ef);
  206.  
  207.    /**
  208.     * Read a specified entry from an eet file and return data
  209.     * @param ef A valid eet file handle opened for reading.
  210.     * @param name Name of the entry. eg: "/base/file_i_want".
  211.     * @param size_ret Number of bytes read from entry and returned.
  212.     * @return The data stored in that entry in the eet file.
  213.     *
  214.     * This function finds an entry in the eet file that is stored under the
  215.     * name specified, and returns that data, decompressed, if successful.
  216.     * NULL is returned if the lookup fails or if memory errors are
  217.     * encountered. It is the job of the calling program to call free() on
  218.     * the returned data. The number of bytes in the returned data chunk are
  219.     * placed in size_ret.
  220.     *
  221.     * If the eet file handle is not valid NULL is returned and size_ret is
  222.     * filled with 0.
  223.     */
  224.    EAPI void     *eet_read  (Eet_File *ef, char *name, int *size_ret);
  225.  
  226.    /**
  227.     * Write a specified entry to an eet file handle
  228.     * @param ef A valid eet file handle opened for writing.
  229.     * @param name Name of the entry. eg: "/base/file_i_want".
  230.     * @param data Pointer to the data to be stored.
  231.     * @param size Length in bytes in the data to be stored.
  232.     * @param compress Compression flags (1 == compress, 0 = don't compress).
  233.     * @return Success or failure of the write.
  234.     *
  235.     * This function will write the specified chunk of data to the eet file
  236.     * and return greater than 0 on success. 0 will be returned on failure.
  237.     *
  238.     * The eet file handle must be a valid file handle for an eet file opened
  239.     * for writing. If it is not, 0 will be returned and no action will be
  240.     * performed.
  241.     *
  242.     * Name, and data must not be NULL, and size must be > 0. If these
  243.     * conditions are not met, 0 will be returned.
  244.     *
  245.     * The data will be copied (and optionally compressed) in ram, pending
  246.     * a flush to disk (it will stay in ram till the eet file handle is
  247.     * closed though).
  248.     */
  249.    EAPI int       eet_write (Eet_File *ef, char *name, void *data, int size, int compress);
  250.  
  251.    /**
  252.     * Delete a specified entry from an Eet file being written or re-written
  253.     * @param ef A valid eet file handle opened for writing.
  254.     * @param name Name of the entry. eg: "/base/file_i_want".
  255.     * @return Success or failure of the delete.
  256.     *
  257.     * This function will delete the specified chunk of data from the eet file
  258.     * and return greater than 0 on success. 0 will be returned on failure.
  259.     *
  260.     * The eet file handle must be a valid file handle for an eet file opened
  261.     * for writing. If it is not, 0 will be returned and no action will be
  262.     * performed.
  263.     *
  264.     * Name, must not be NULL, otherwise 0 will be returned.
  265.     */
  266.    EAPI int       eet_delete(Eet_File *ef, char *name);
  267.  
  268.    /**
  269.     * List all entries in eet file matching shell glob.
  270.     * @param ef A valid eet file handle.
  271.     * @param glob A shell glob to match against.
  272.     * @param count_ret Number of entries found to match.
  273.     * @return Pointer to an array of strings.
  274.     *
  275.     * This function will list all entries in the eet file matching the
  276.     * supplied shell glob and return an allocated list of their names, if
  277.     * there are any, and if no memory errors occur.
  278.     *
  279.     * The eet file handle must be valid and glob must not be NULL, or NULL
  280.     * will be returned and count_ret will be filled with 0.
  281.     *
  282.     * The calling program must call free() on the array returned, but NOT
  283.     * on the string pointers in the array. They are taken as read-only
  284.     * internals from the eet file handle. They are only valid as long as
  285.     * the file handle is not closed. When it is closed those pointers in the
  286.     * array are now not valid and should not be used.
  287.     *
  288.     * On success the array returned will have a list of string pointers
  289.     * that are the names of the entries that matched, and count_ret will have
  290.     * the number of entries in this array placed in it.
  291.     *
  292.     * Hint: an easy way to list all entries in an eet file is to use a glob
  293.     * value of "*".
  294.     */
  295.    EAPI char    **eet_list  (Eet_File *ef, char *glob, int *count_ret);
  296.  
  297.    /**
  298.     * Return the number of entries in the specified eet file.
  299.     * @param ef A valid eet file handle.
  300.     * @return Number of entries in ef or -1 if the number of entries
  301.     *         cannot be read due to open mode restrictions.
  302.     */
  303.    EAPI int       eet_num_entries(Eet_File *ef);
  304.  
  305. /***************************************************************************/
  306.  
  307.    /**
  308.     * Read just the header data for an image and dont decode the pixels.
  309.     * @param ef A valid eet file handle opened for reading.
  310.     * @param name Name of the entry. eg: "/base/file_i_want".
  311.     * @param w A pointer to the unsigned int to hold the width in pixels.
  312.     * @param h A pointer to the unsigned int to hold the height in pixels.
  313.     * @param alpha A pointer to the int to hold the alpha flag.
  314.     * @param compress A pointer to the int to hold the compression amount.
  315.     * @param quality A pointer to the int to hold the quality amount.
  316.     * @param lossy A pointer to the int to hold the lossiness flag.
  317.     * @return 1 on successfull decode, 0 otherwise
  318.     *
  319.     * This function reads an image from an eet file stored under the named
  320.     * key in the eet file and return a pointer to the decompressed pixel data.
  321.     *
  322.     * The other parameters of the image (width, height etc.) are placed into
  323.     * the values pointed to (they must be supplied). The pixel data is a linear
  324.     * array of pixels starting from the top-left of the image scanning row by
  325.     * row from left to right. Each pile is a 32bit value, with the high byte
  326.     * being the alpha channel, the next being red, then green, and the low byte
  327.     * being blue. The width and height are measured in pixels and will be
  328.     * greater than 0 when returned. The alpha flag is either 0 or 1. 0 denotes
  329.     * that the alpha channel is not used. 1 denotes that it is significant.
  330.     * Compress is filled with the compression value/amount the image was
  331.     * stored with. The quality value is filled with the quality encoding of
  332.     * the image file (0 - 100). The lossy flags is either 0 or 1 as to if
  333.     * the image was encoded lossily or not.
  334.     *
  335.     * On success the function returns 1 indicating the header was read and
  336.     * decoded properly, or 0 on failure.
  337.     */
  338.    EAPI int       eet_data_image_header_read(Eet_File *ef, char *name, unsigned int *w, unsigned int *h, int *alpha, int *compress, int *quality, int *lossy);
  339.  
  340.    /**
  341.     * Read image data from the named key in the eet file.
  342.     * @param ef A valid eet file handle opened for reading.
  343.     * @param name Name of the entry. eg: "/base/file_i_want".
  344.     * @param w A pointer to the unsigned int to hold the width in pixels.
  345.     * @param h A pointer to the unsigned int to hold the height in pixels.
  346.     * @param alpha A pointer to the int to hold the alpha flag.
  347.     * @param compress A pointer to the int to hold the compression amount.
  348.     * @param quality A pointer to the int to hold the quality amount.
  349.     * @param lossy A pointer to the int to hold the lossiness flag.
  350.     * @return The image pixel data decoded
  351.     *
  352.     * This function reads an image from an eet file stored under the named
  353.     * key in the eet file and return a pointer to the decompressed pixel data.
  354.     *
  355.     * The other parameters of the image (width, height etc.) are placed into
  356.     * the values pointed to (they must be supplied). The pixel data is a linear
  357.     * array of pixels starting from the top-left of the image scanning row by
  358.     * row from left to right. Each pile is a 32bit value, with the high byte
  359.     * being the alpha channel, the next being red, then green, and the low byte
  360.     * being blue. The width and height are measured in pixels and will be
  361.     * greater than 0 when returned. The alpha flag is either 0 or 1. 0 denotes
  362.     * that the alpha channel is not used. 1 denotes that it is significant.
  363.     * Compress is filled with the compression value/amount the image was
  364.     * stored with. The quality value is filled with the quality encoding of
  365.     * the image file (0 - 100). The lossy flags is either 0 or 1 as to if
  366.     * the image was encoded lossily or not.
  367.     *
  368.     * On success the function returns a pointer to the image data decoded. The
  369.     * calling application is responsible for calling free() on the image data
  370.     * when it is done with it. On failure NULL is returned and the parameter
  371.     * values may not contain any sensible data.
  372.     */
  373.    EAPI void     *eet_data_image_read(Eet_File *ef, char *name, unsigned int *w, unsigned int *h, int *alpha, int *compress, int *quality, int *lossy);
  374.  
  375.    /**
  376.     * Write image data to the named key in an eet file.
  377.     * @param ef A valid eet file handle opened for writing.
  378.     * @param name Name of the entry. eg: "/base/file_i_want".
  379.     * @param data A pointer to the image pixel data.
  380.     * @param w The width of the image in pixels.
  381.     * @param h The height of the image in pixels.
  382.     * @param alpha The alpha channel flag.
  383.     * @param compress The compression amount.
  384.     * @param quality The quality encoding amount.
  385.     * @param lossy The lossiness flag.
  386.     * @return Success if the data was encoded and written or not.
  387.     *
  388.     * This function takes image pixel data and encodes it in an eet file
  389.     * stored under the supplied name key, and returns how many bytes were
  390.     * actually written to encode the image data.
  391.     *
  392.     * The data expected is the same format as returned by eet_data_image_read.
  393.     * If this is not the case weird things may happen. Width and height must
  394.     * be between 1 and 8000 pixels. The alpha flags can be 0 or 1 (0 meaning
  395.     * the alpha values are not useful and 1 meaning they are). Compress can
  396.     * be from 0 to 9 (0 meaning no compression, 9 meaning full compression).
  397.     * This is only used if the image is not lossily encoded. Quality is used on
  398.     * lossy compression and should be a value from 0 to 100. The lossy flag
  399.     * can be 0 or 1. 0 means encode losslessly and 1 means to encode with
  400.     * image quality loss (but then have a much smaller encoding).
  401.     *
  402.     * On success this function returns the number of bytes that were required
  403.     * to encode the image data, or on failure it returns 0.
  404.     */
  405.    EAPI int       eet_data_image_write(Eet_File *ef, char *name, void *data, unsigned int w, unsigned int h, int alpha, int compress, int quality, int lossy);
  406.  
  407.    /**
  408.     * Decode Image data header only to get information.
  409.     * @param data The encoded pixel data.
  410.     * @param size The size, in bytes, of the encoded pixel data.
  411.     * @param w A pointer to the unsigned int to hold the width in pixels.
  412.     * @param h A pointer to the unsigned int to hold the height in pixels.
  413.     * @param alpha A pointer to the int to hold the alpha flag.
  414.     * @param compress A pointer to the int to hold the compression amount.
  415.     * @param quality A pointer to the int to hold the quality amount.
  416.     * @param lossy A pointer to the int to hold the lossiness flag.
  417.     * @return 1 on success, 0 on failure.
  418.     *
  419.     * This function takes encoded pixel data and decodes it into raw RGBA
  420.     * pixels on success.
  421.     *
  422.     * The other parameters of the image (width, height etc.) are placed into
  423.     * the values pointed to (they must be supplied). The pixel data is a linear
  424.     * array of pixels starting from the top-left of the image scanning row by
  425.     * row from left to right. Each pixel is a 32bit value, with the high byte
  426.     * being the alpha channel, the next being red, then green, and the low byte
  427.     * being blue. The width and height are measured in pixels and will be
  428.     * greater than 0 when returned. The alpha flag is either 0 or 1. 0 denotes
  429.     * that the alpha channel is not used. 1 denotes that it is significant.
  430.     * Compress is filled with the compression value/amount the image was
  431.     * stored with. The quality value is filled with the quality encoding of
  432.     * the image file (0 - 100). The lossy flags is either 0 or 1 as to if
  433.     * the image was encoded lossily or not.
  434.     *
  435.     * On success the function returns 1 indicating the header was read and
  436.     * decoded properly, or 0 on failure.
  437.     */
  438.    EAPI int       eet_data_image_header_decode(void *data, int size, unsigned int *w, unsigned int *h, int *alpha, int *compress, int *quality, int *lossy);
  439.  
  440.    /**
  441.     * Decode Image data into pixel data.
  442.     * @param data The encoded pixel data.
  443.     * @param size The size, in bytes, of the encoded pixel data.
  444.     * @param w A pointer to the unsigned int to hold the width in pixels.
  445.     * @param h A pointer to the unsigned int to hold the height in pixels.
  446.     * @param alpha A pointer to the int to hold the alpha flag.
  447.     * @param compress A pointer to the int to hold the compression amount.
  448.     * @param quality A pointer to the int to hold the quality amount.
  449.     * @param lossy A pointer to the int to hold the lossiness flag.
  450.     * @return The image pixel data decoded
  451.     *
  452.     * This function takes encoded pixel data and decodes it into raw RGBA
  453.     * pixels on success.
  454.     *
  455.     * The other parameters of the image (width, height etc.) are placed into
  456.     * the values pointed to (they must be supplied). The pixel data is a linear
  457.     * array of pixels starting from the top-left of the image scanning row by
  458.     * row from left to right. Each pixel is a 32bit value, with the high byte
  459.     * being the alpha channel, the next being red, then green, and the low byte
  460.     * being blue. The width and height are measured in pixels and will be
  461.     * greater than 0 when returned. The alpha flag is either 0 or 1. 0 denotes
  462.     * that the alpha channel is not used. 1 denotes that it is significant.
  463.     * Compress is filled with the compression value/amount the image was
  464.     * stored with. The quality value is filled with the quality encoding of
  465.     * the image file (0 - 100). The lossy flags is either 0 or 1 as to if
  466.     * the image was encoded lossily or not.
  467.     *
  468.     * On success the function returns a pointer to the image data decoded. The
  469.     * calling application is responsible for calling free() on the image data
  470.     * when it is done with it. On failure NULL is returned and the parameter
  471.     * values may not contain any sensible data.
  472.     */
  473.    EAPI void     *eet_data_image_decode(void *data, int size, unsigned int *w, unsigned int *h, int *alpha, int *compress, int *quality, int *lossy);
  474.  
  475.    /**
  476.     * Encode image data for storage or transmission.
  477.     * @param data A pointer to the image pixel data.
  478.     * @param size_ret A pointer to an int to hold the size of the returned data.
  479.     * @param w The width of the image in pixels.
  480.     * @param h The height of the image in pixels.
  481.     * @param alpha The alpha channel flag.
  482.     * @param compress The compression amount.
  483.     * @param quality The quality encoding amount.
  484.     * @param lossy The lossiness flag.
  485.     * @return The encoded image data.
  486.     *
  487.     * This function stakes image pixel data and encodes it with compression and
  488.     * possible loss of quality (as a trade off for size) for storage or
  489.     * transmission to another system.
  490.     *
  491.     * The data expected is the same format as returned by eet_data_image_read.
  492.     * If this is not the case weird things may happen. Width and height must
  493.     * be between 1 and 8000 pixels. The alpha flags can be 0 or 1 (0 meaning
  494.     * the alpha values are not useful and 1 meaning they are). Compress can
  495.     * be from 0 to 9 (0 meaning no compression, 9 meaning full compression).
  496.     * This is only used if the image is not lossily encoded. Quality is used on
  497.     * lossy compression and should be a value from 0 to 100. The lossy flag
  498.     * can be 0 or 1. 0 means encode losslessly and 1 means to encode with
  499.     * image quality loss (but then have a much smaller encoding).
  500.     *
  501.     * On success this function returns a pointer to the encoded data that you
  502.     * can free with free() when no longer needed.
  503.     */
  504.    EAPI void     *eet_data_image_encode(void *data, int *size_ret, unsigned int w, unsigned int h, int alpha, int compress, int quality, int lossy);
  505.  
  506. /***************************************************************************/
  507.  
  508.    /**
  509.     * Create a new empty data structure descriptor.
  510.     * @param name The string name of this data structure (most be a global constant and never change).
  511.     * @param size The size of the struct (in bytes).
  512.     * @param func_list_next The function to get the next list node.
  513.     * @param func_list_append The function to append a member to a list.
  514.     * @param func_list_data The function to get the data from a list node.
  515.     * @param func_list_free The function to free an entire linked list.
  516.     * @param func_hash_foreach The function to iterate through all hash table entries.
  517.     * @param func_hash_add The function to add a member to a hash table.
  518.     * @param func_hash_free The function to free an entire hash table.
  519.     * @return A new empty data descriptor.
  520.     *
  521.     * This function creates a new data descriptore and returns a handle to the
  522.     * new data descriptor. On creation it will be empty, containing no contents
  523.     * describing anything other than the shell of the data structure.
  524.     *
  525.     * You add structure members to the data descriptor using the macros
  526.     * EET_DATA_DESCRIPTOR_ADD_BASIC(), EET_DATA_DESCRIPTOR_ADD_SUB() and
  527.     * EET_DATA_DESCRIPTOR_ADD_LIST(), depending on what type of member you are
  528.     * adding to the description.
  529.     *
  530.     * Once you have described all the members of a struct you want loaded, or
  531.     * saved eet can load and save those members for you, encode them into
  532.     * endian-independant serialised data chunks for transmission across a
  533.     * a network or more.
  534.     *
  535.     * Example:
  536.     *
  537.     * @code
  538.     * #include <Eet.h>
  539.     * #include <Evas.h>
  540.     *
  541.     * typedef struct _blah2
  542.     * {
  543.     *    char *string;
  544.     * }
  545.     * Blah2;
  546.     *
  547.     * typedef struct _blah3
  548.     * {
  549.     *    char *string;
  550.     * }
  551.     * Blah3;
  552.     *
  553.     * typedef struct _blah
  554.     * {
  555.     *    char character;
  556.     *    short sixteen;
  557.     *    int integer;
  558.     *    long long lots;
  559.     *    float floating;
  560.     *    double floating_lots;
  561.     *    char *string;
  562.     *    Blah2 *blah2;
  563.     *    Evas_List *blah3;
  564.     * }
  565.     * Blah;
  566.     *
  567.     * int
  568.     * main(int argc, char **argv)
  569.     * {
  570.     *    Blah blah;
  571.     *    Blah2 blah2;
  572.     *    Blah3 blah3;
  573.     *    Eet_Data_Descriptor *edd, *edd2, *edd3;
  574.     *    void *data;
  575.     *    int size;
  576.     *    FILE *f;
  577.     *    Blah *blah_in;
  578.     *
  579.     *    edd3 = eet_data_descriptor_new("blah3", sizeof(Blah3),
  580.     *                                   evas_list_next,
  581.     *                                   evas_list_append,
  582.     *                                   evas_list_data,
  583.     *                                   evas_list_free,
  584.     *                                   evas_hash_foreach,
  585.     *                                   evas_hash_add,
  586.     *                                   evas_hash_free);
  587.     *    EET_DATA_DESCRIPTOR_ADD_BASIC(edd3, Blah3, "string3", string, EET_T_STRING);
  588.     *
  589.     *    edd2 = eet_data_descriptor_new("blah2", sizeof(Blah2),
  590.     *                                   evas_list_next,
  591.     *                                   evas_list_append,
  592.     *                                   evas_list_data,
  593.     *                                   evas_list_free,
  594.     *                                   evas_hash_foreach,
  595.     *                                   evas_hash_add,
  596.     *                                   evas_hash_free);
  597.     *    EET_DATA_DESCRIPTOR_ADD_BASIC(edd2, Blah2, "string2", string, EET_T_STRING);
  598.     *
  599.     *    edd = eet_data_descriptor_new("blah", sizeof(Blah),
  600.     *                                   evas_list_next,
  601.     *                                   evas_list_append,
  602.     *                                   evas_list_data,
  603.     *                                   evas_list_free,
  604.     *                                   evas_hash_foreach,
  605.     *                                   evas_hash_add,
  606.     *                                   evas_hash_free);
  607.     *    EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Blah, "character", character, EET_T_CHAR);
  608.     *    EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Blah, "sixteen", sixteen, EET_T_SHORT);
  609.     *    EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Blah, "integer", integer, EET_T_INT);
  610.     *    EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Blah, "lots", lots, EET_T_LONG_LONG);
  611.     *    EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Blah, "floating", floating, EET_T_FLOAT);
  612.     *    EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Blah, "floating_lots", floating_lots, EET_T_DOUBLE);
  613.     *    EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Blah, "string", string, EET_T_STRING);
  614.     *    EET_DATA_DESCRIPTOR_ADD_SUB(edd, Blah, "blah2", blah2, edd2);
  615.     *    EET_DATA_DESCRIPTOR_ADD_LIST(edd, Blah, "blah3", blah3, edd3);
  616.     *
  617.     *    blah3.string="PANTS";
  618.     *
  619.     *    blah2.string="subtype string here!";
  620.     *
  621.     *    blah.character='7';
  622.     *    blah.sixteen=0x7777;
  623.     *    blah.integer=0xc0def00d;
  624.     *    blah.lots=0xdeadbeef31337777;
  625.     *    blah.floating=3.141592654;
  626.     *    blah.floating_lots=0.777777777777777;
  627.     *    blah.string="bite me like a turnip";
  628.     *    blah.blah2 = &blah2;
  629.     *    blah.blah3 = evas_list_append(NULL, &blah3);
  630.     *    blah.blah3 = evas_list_append(blah.blah3, &blah3);
  631.     *    blah.blah3 = evas_list_append(blah.blah3, &blah3);
  632.     *    blah.blah3 = evas_list_append(blah.blah3, &blah3);
  633.     *    blah.blah3 = evas_list_append(blah.blah3, &blah3);
  634.     *    blah.blah3 = evas_list_append(blah.blah3, &blah3);
  635.     *    blah.blah3 = evas_list_append(blah.blah3, &blah3);
  636.     *
  637.     *    data = eet_data_descriptor_encode(edd, &blah, &size);
  638.     *    f = fopen("out", "w");
  639.     *    if (f)
  640.     *      {
  641.     *         fwrite(data, size, 1, f);
  642.     *         fclose(f);
  643.     *      }
  644.     *    printf("-----DECODING\n");
  645.     *    blah_in = eet_data_descriptor_decode(edd, data, size);
  646.     *
  647.     *    printf("-----DECODED!\n");
  648.     *    printf("%c\n", blah_in->character);
  649.     *    printf("%x\n", (int)blah_in->sixteen);
  650.     *    printf("%x\n", blah_in->integer);
  651.     *    printf("%lx\n", blah_in->lots);
  652.     *    printf("%f\n", (double)blah_in->floating);
  653.     *    printf("%f\n", (double)blah_in->floating_lots);
  654.     *    printf("%s\n", blah_in->string);
  655.     *    printf("%p\n", blah_in->blah2);
  656.     *    printf("  %s\n", blah_in->blah2->string);
  657.     *      {
  658.     *         Evas_List *l;
  659.     *
  660.     *         for (l = blah_in->blah3; l; l = l->next)
  661.     *           {
  662.     *              Blah3 *blah3_in;
  663.     *
  664.     *              blah3_in = l->data;
  665.     *              printf("%p\n", blah3_in);
  666.     *              printf("  %s\n", blah3_in->string);
  667.     *           }
  668.     *      }
  669.     *    eet_data_descriptor_free(edd);
  670.     *    eet_data_descriptor_free(edd2);
  671.     *    eet_data_descriptor_free(edd3);
  672.     *
  673.     *   return 0;
  674.     * }
  675.     *
  676.     * @endcode
  677.     *
  678.     */
  679.    EAPI Eet_Data_Descriptor *eet_data_descriptor_new(const char *name, int size, void *(*func_list_next) (void *l), void *(*func_list_append) (void *l, void *d), void *(*func_list_data) (void *l), void *(*func_list_free) (void *l), void  (*func_hash_foreach) (void *h, int (*func) (void *h, const char *k, void *dt, void *fdt), void *fdt), void *(*func_hash_add) (void *h, const char *k, void *d), void  (*func_hash_free) (void *h));
  680.    /*
  681.     * FIXME:
  682.     * 
  683.     * moving to this api from the old above. this will break things when the
  684.     * move happens - but be warned
  685.     */
  686.    EAPI Eet_Data_Descriptor *eet_data_descriptor2_new(Eet_Data_Descriptor_Class *eddc);
  687.        
  688.    /**
  689.     * This function frees a data descriptor when it is not needed anymore.
  690.     * @param edd The data descriptor to free.
  691.     *
  692.     * This function takes a data descriptor handle as a parameter and frees all
  693.     * data allocated for the data descriptor and the handle itself. After this
  694.     * call the descriptor is no longer valid.
  695.     *
  696.     */
  697.    EAPI void                 eet_data_descriptor_free(Eet_Data_Descriptor *edd);
  698.  
  699.    /**
  700.     * This function is an internal used by macros.
  701.     *
  702.     * This function is used by macros EET_DATA_DESCRIPTOR_ADD_BASIC(),
  703.     * EET_DATA_DESCRIPTOR_ADD_SUB() and EET_DATA_DESCRIPTOR_ADD_LIST(). It is
  704.     * complex to use by hand and should be left to be used by the macros, and
  705.     * thus is not documented.
  706.     *
  707.     */
  708.    EAPI void  eet_data_descriptor_element_add(Eet_Data_Descriptor *edd, const char *name, int type, int group_type, int offset, int count, const char *counter_name, Eet_Data_Descriptor *subtype);
  709.  
  710.    /**
  711.     * Read a data structure from an eet file and decodes it.
  712.     * @param ef The eet file handle to read from.
  713.     * @param edd The data descriptor handle to use when decoding.
  714.     * @param name The key the data is stored under in the eet file.
  715.     * @return A pointer to the decoded data structure.
  716.     *
  717.     * This function decodes a data structure stored in an eet file, returning
  718.     * a pointer to it if it decoded successfully, or NULL on failure. This
  719.     * can save a programmer dozens of hours of work in writing configuration
  720.     * file parsing and writing code, as eet does all that work for the program
  721.     * and presents a program-friendly data structure, just as the programmer
  722.     * likes. Eet can handle members being added or deleted from the data in
  723.     * storage and safely zero-fills unfilled members if they were not found
  724.     * in the data. It checks sizes and headers whenever it reads data, allowing
  725.     * the programmer to not worry about corrupt data.
  726.     *
  727.     * Once a data structure has been described by the programmer with the
  728.     * fields they wish to save or load, storing or retrieving a data structure
  729.     * from an eet file, or from a chunk of memory is as simple as a single
  730.     * function call.
  731.     *
  732.     */
  733.    EAPI void *eet_data_read(Eet_File *ef, Eet_Data_Descriptor *edd, char *name);
  734.  
  735.    /**
  736.     * Write a data structure from memory and store in an eet file.
  737.     * @param ef The eet file handle to write to.
  738.     * @param edd The data descriptor to use when encoding.
  739.     * @param name The key to store the data under in the eet file.
  740.     * @param data A pointer to the data structure to ssave and encode.
  741.     * @param compress Compression flags for storage.
  742.     * @return 1 on successful write, 0 on failure.
  743.     *
  744.     * This function is the reverse of eet_data_read(), saving a data structure
  745.     * to an eet file.
  746.     *
  747.     */
  748.    EAPI int   eet_data_write(Eet_File *ef, Eet_Data_Descriptor *edd, char *name, void *data, int compress);
  749.  
  750.    /**
  751.     * Decode a data structure from an arbitary location in memory.
  752.     * @param edd The data  descriptor to use when decoding.
  753.     * @param data_in The pointer to the data to decode into a struct.
  754.     * @param size_in The size of the data pointed to in bytes.
  755.     * @return NULL on failure, or a valid decoded struct pointer on success.
  756.     *
  757.     * This function will decode a data structure that has been encoded using
  758.     * eet_data_descriptor_encode(), and return a data structure with all its
  759.     * elements filled out, if successful, or NULL on failure.
  760.     *
  761.     * The data to be decoded is stored at the memory pointed to by @p data_in,
  762.     * and is described by the descriptor pointed to by @p edd. The data size is
  763.     * passed in as the value to @p size_in, ande must be greater than 0 to
  764.     * succeed.
  765.     *
  766.     * This function is useful for decoding data structures delivered to the
  767.     * application by means other than an eet file, such as an IPC or socket
  768.     * connection, raw files, shared memory etc.
  769.     *
  770.     * Please see eet_data_read() for more information.
  771.     *
  772.     */
  773.    EAPI void *eet_data_descriptor_decode(Eet_Data_Descriptor *edd, void *data_in, int size_in);
  774.  
  775.    /**
  776.     * Encode a dsata struct to memory and return that encoded data.
  777.     * @param edd The data  descriptor to use when encoding.
  778.     * @param data_in The pointer to the struct to encode into data.
  779.     * @param size_ret A pointer to the an int to be filled with the decoded size.
  780.     * @return NULL on failure, or a valid encoded data chunk on success.
  781.     *
  782.     * This function takes a data structutre in memory and encodes it into a
  783.     * serialised chunk of data that can be decoded again by
  784.     * eet_data_descriptor_decode(). This is useful for being able to transmit
  785.     * data structures across sockets, pipes, IPC or shared file mechanisms,
  786.     * without having to worry about memory space, machine type, endianess etc.
  787.     *
  788.     * The parameter @p edd must point to a valid data descriptor, and
  789.     * @p data_in must point to the right data structure to encode. If not, the
  790.     * encoding may fail.
  791.     *
  792.     * On success a non NULL valid pointer is returned and what @p size_ret
  793.     * points to is set to the size of this decoded data, in bytes. When the
  794.     * encoded data is no longer needed, call free() on it. On failure NULL is
  795.     * returned and what @p size_ret points to is set to 0.
  796.     *
  797.     * Please see eet_data_write() for more information.
  798.     *
  799.     */
  800.    EAPI void *eet_data_descriptor_encode(Eet_Data_Descriptor *edd, void *data_in, int *size_ret);
  801.  
  802.    /**
  803.     * Add a basic data element to a data descriptor.
  804.     * @param edd The data descriptor to add the type to.
  805.     * @param struct_type The type of the struct.
  806.     * @param name The string name to use to encode/decode this member (must be a constant global and never change).
  807.     * @param member The struct member itself to be encoded.
  808.     * @param type The type of the member to encode.
  809.     *
  810.     * This macro is a convenience macro provided to add a member to the data
  811.     * descriptor @p edd. The type of the structure is provided as the
  812.     * @p struct_type parameter (for example: struct my_struct). The @p name
  813.     * parameter defines a string that will be used to uniquely name that
  814.     * member of the struct (it is suggested to use the struct member itself).
  815.     * The @p member parameter is the actual struct member itself (for
  816.     * example: values), and @p type is the basic data type of the member which
  817.     * must be one of: EET_T_CHAR, EET_T_SHORT, EET_T_INT, EET_T_LONG_LONG,
  818.     * EET_T_FLOAT, EET_T_DOUBLE, EET_T_UCHAR, EET_T_USHORT, EET_T_UINT,
  819.     * EET_T_ULONG_LONG or EET_T_STRING.
  820.     *
  821.     */
  822. #define EET_DATA_DESCRIPTOR_ADD_BASIC(edd, struct_type, name, member, type) \
  823.      { \
  824.     struct_type ___ett; \
  825.     \
  826.     eet_data_descriptor_element_add(edd, name, type, EET_G_UNKNOWN, \
  827.                     (char *)(&(___ett.member)) - (char *)(&(___ett)), \
  828.                     0, NULL, NULL); \
  829.      }
  830.  
  831.    /**
  832.     * Add a sub-element type to a data descriptor
  833.     * @param edd The data descriptor to add the type to.
  834.     * @param struct_type The type of the struct.
  835.     * @param name The string name to use to encode/decode this member (must be a constant global and never change).
  836.     * @param member The struct member itself to be encoded.
  837.     * @param subtype The type of sub-type struct to add.
  838.     *
  839.     * This macro lets you easily add a sub-type (a struct that's pointed to
  840.     * by this one). All the parameters are the same as for
  841.     * EET_DATA_DESCRIPTOR_ADD_BASIC(), with the @p subtype being the exception.
  842.     * This must be the data descriptor of the struct that is pointed to by
  843.     * this element.
  844.     *
  845.     */
  846. #define EET_DATA_DESCRIPTOR_ADD_SUB(edd, struct_type, name, member, subtype) \
  847.      { \
  848.     struct_type ___ett; \
  849.     \
  850.     eet_data_descriptor_element_add(edd, name, EET_T_UNKNOW, EET_G_UNKNOWN, \
  851.                     (char *)(&(___ett.member)) - (char *)(&(___ett)), \
  852.                     0, NULL, subtype); \
  853.      }
  854.  
  855.    /**
  856.     * Add a linked list type to a data descriptor
  857.     * @param edd The data descriptor to add the type to.
  858.     * @param struct_type The type of the struct.
  859.     * @param name The string name to use to encode/decode this member (must be a constant global and never change).
  860.     * @param member The struct member itself to be encoded.
  861.     * @param subtype The type of linked list member to add.
  862.     *
  863.     * This macro lets you easily add a linked list of other data types. All the
  864.     * parameters are the same as for EET_DATA_DESCRIPTOR_ADD_BASIC(), with the
  865.     * @p subtype being the exception. This must be the data descriptor of the
  866.     * element that is in each member of the linked list to be stored.
  867.     *
  868.     */
  869. #define EET_DATA_DESCRIPTOR_ADD_LIST(edd, struct_type, name, member, subtype) \
  870.      { \
  871.     struct_type ___ett; \
  872.     \
  873.     eet_data_descriptor_element_add(edd, name, EET_T_UNKNOW, EET_G_LIST, \
  874.                     (char *)(&(___ett.member)) - (char *)(&(___ett)), \
  875.                     0, NULL, subtype); \
  876.      }
  877.  
  878. /***************************************************************************/
  879. #ifdef __cplusplus
  880. }
  881. #endif
  882.  
  883. #endif
  884.